package ray.create;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

import java.util.List;

public class SelectByBeanPlugin extends PluginAdapter {
	
	String sqlId = "selectByRecord";
   
	@Override
	public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
                                   IntrospectedTable introspectedTable) {
        FullyQualifiedJavaType javaType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
        Method m = new Method(sqlId);
        m.setVisibility(JavaVisibility.DEFAULT);
        returnType.addTypeArgument(javaType);
	        m.setReturnType(returnType);
        m.addParameter(new Parameter(javaType, "record"));
        context.getCommentGenerator().addGeneralMethodComment(m, introspectedTable);
	        interfaze.addMethod(m);
	        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
	}
	


	/** 
     * 生成mapping 添加自定义sql 
     */  
    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {  
       // String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();// 数据库表名  
       // List<IntrospectedColumn> columns = introspectedTable.getAllColumns();  
        XmlElement parentElement = document.getRootElement();  
                         
        //添加getList  
        XmlElement select = new XmlElement("select");
        select.addAttribute(new Attribute("id", sqlId));
        select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        select.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
        select.addElement(new TextElement(" select  <include refid=\"Base_Column_List\" />  from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));

        XmlElement where = new XmlElement("where");  
        StringBuilder sb = new StringBuilder();  
        for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
            //$NON-NLS-1$
            XmlElement isNotNullElement = new XmlElement("if");
            sb.setLength(0);
            sb.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb.append(" != null");
            //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", sb.toString()));
            where.addElement(isNotNullElement);  
  
            sb.setLength(0);  
            sb.append(" and ");  
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            //$NON-NLS-1$
            sb.append(" = ");
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
            isNotNullElement.addElement(new TextElement(sb.toString()));  
        }

        select.addElement(where);  
        parentElement.addElement(select);  
          
        return super.sqlMapDocumentGenerated(document, introspectedTable);  
    }  
    



	@Override
    public boolean validate(List<String> arg0) {
        return true;
	} 
}
